'''
Created on 28/03/2010

@author: miguel
'''
import pygame
from pygame.sprite import Sprite
from vec2d import vec2d

class BugManual(Sprite):
    (ALIVE, DEAD) = range(2)
    (UP, DOWN, LEFT, RIGHT) = range(4)
    BUG_IMAGE = './data/bug.png'

    def __init__(self, screen, game, field, init_position, owner):
        Sprite.__init__(self)

        self.screen = screen
        self.game = game
        self.field = field
        self.pos = vec2d(init_position)
        self.initial_image = pygame.image.load(self.BUG_IMAGE)
        self.image = self.initial_image
        self.direction = self.UP
        self.movement_counter = 0
        self.owner = owner
        self.game.game_board.take_position(self.pos, owner)

    def update(self):
        pass

    def go_up(self):
        if self.movement_counter == 0:
            self.old_pos = self.pos
            self.new_pos = self.pos + vec2d(0, -30)
            if self.game.game_board.is_position_available(self.new_pos):
                self.movement = True
                self.game.game_board.take_position(self.new_pos, self.game.game_board.USER_BUG)
            else:
                self.movement = False
        if self.movement == True:
            self.pos = vec2d(self.pos.x, self.pos.y - 15)
            self.movement_counter = self.movement_counter + 15
        else:
            pass
        if self.movement_counter >= 30:
            if self.movement == True:
                self.game.game_board.release_position(self.old_pos)
                self.movement_counter = 0

        if self.direction != self.UP:
            self.image = self.initial_image
        self.direction = self.UP

        self.game.game_board.print_board()

    def go_down(self):
        if self.movement_counter == 0:
            self.old_pos = self.pos
            self.new_pos = self.pos + vec2d(0, 30)
            if self.game.game_board.is_position_available(self.new_pos):
                self.movement = True
                self.game.game_board.take_position(self.new_pos, self.game.game_board.USER_BUG)
            else:
                self.movement = False
        if self.movement == True:
            self.pos = vec2d(self.pos.x, self.pos.y + 15)
            self.movement_counter = self.movement_counter + 15
        else:
            pass
        if self.movement_counter >= 30:
            if self.movement == True:
                self.game.game_board.release_position(self.old_pos)
                self.movement_counter = 0

        if self.direction != self.DOWN:
            self.image = pygame.transform.rotate(self.initial_image, 180)
        self.direction = self.DOWN

        self.game.game_board.print_board()

    def go_left(self):
        if self.movement_counter == 0:
            self.old_pos = self.pos
            self.new_pos = self.pos + vec2d(-30, 0)
            if self.game.game_board.is_position_available(self.new_pos):
                self.movement = True
                self.game.game_board.take_position(self.new_pos, self.game.game_board.USER_BUG)
            else:
                self.movement = False
        if self.movement == True:
            self.pos = vec2d(self.pos.x - 15, self.pos.y)
            self.movement_counter = self.movement_counter + 15
        else:
            pass
        if self.movement_counter >= 30:
            if self.movement == True:
                self.game.game_board.release_position(self.old_pos)
                self.movement_counter = 0

        if self.direction != self.LEFT:
            self.image = pygame.transform.rotate(self.initial_image, 90)
        self.direction = self.LEFT

        self.game.game_board.print_board()

    def go_right(self):
        if self.movement_counter == 0:
            self.old_pos = self.pos
            self.new_pos = self.pos + vec2d(30, 0)
            if self.game.game_board.is_position_available(self.new_pos):
                self.movement = True
                self.game.game_board.take_position(self.new_pos, self.game.game_board.USER_BUG)
            else:
                self.movement = False
        if self.movement == True:
            self.pos = vec2d(self.pos.x + 15, self.pos.y)
            self.movement_counter = self.movement_counter + 15
        else:
            pass
        if self.movement_counter >= 30:
            if self.movement == True:
                self.game.game_board.release_position(self.old_pos)
                self.movement_counter = 0

        if self.direction != self.RIGHT:
            self.image = pygame.transform.rotate(self.initial_image, 270)
        self.direction = self.RIGHT

        self.game.game_board.print_board()

    def draw(self):
        self.draw_rect = self.image.get_rect().move(self.pos)
        self.screen.blit(self.image, self.draw_rect)